<?php

namespace app\admin\controller;

class CustomerBase
{
    public function index()
    {
        $query = \app\model\Customer::field(['customer.id', 'company_name', 'province_id', 'invoice_tax', 'customer.created_at', 'sales_id', 'customer_follow_id'])->with(['follow', 'salesman'])->order('id', 'desc');

        if (request()->has('companyName')) {
            $query->whereLike('company_name', '%' . input('companyName') . '%');
        }

        if (request()->has('provinceId')) {
            $query->where('province_id', input('provinceId'));
        }

        if (request()->has('createdAt')) {
            $query->whereBetween('created_at', input('createdAt'));
        }

        if (request()->has('salesId')) {
            $query->where('sales_id', input('salesId'));
        }

        if (request()->has('statusValue')) {
            $query->hasWhere('follow', ['status_value' => input('statusValue')]);
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['customers' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Customer::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Customer::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $model = \app\model\Customer::create(input());

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $model = \app\model\Customer::field('id', 'company_name')->where('id', input('id'))->find();

        return ['code' => 20000, 'data' => ['customer' => $model]];
    }
}
